﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.DALFactory;
using AspNetCs.SQLServerDAL;
using AspNetCs.Model;
using AspNetCs.IDAL;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using AspNetCs.Config;
using AspNetCs.Enumerations;
using System.Web;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;

namespace AspNetCs.Bll
{
    public class NetMuBLL
    {
        private INetMu dal = Conn.CreateNetMu();


        /// <summary>
        /// 生成公墓表中不存在的随机数
        /// </summary>
        /// <param name="list">不允许出现的随机数</param>
        /// <returns></returns>
        public string RndNum(ArrayList list, int Rndlen)
        {
            return dal.RndNum(list, Rndlen);
        }

        /// <summary>
        /// 是否存在公墓编号
        /// </summary>
        /// <param name="MuId">公墓编号</param>
        /// <returns></returns>
        public bool IsExistNetMuId(string MuId)
        {
            return dal.IsExistNetMuId(MuId);
        }

        /// <summary>
        /// 添加网墓
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddNetMu(NetMu_Model model)
        {
            return dal.AddNetMu(model);
        }

        /// <summary>
        ///  获得用户创建墓
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public DataTable getMyCreateMetMus(int UserId, string propertyItems)
        {
            return dal.getMyCreateMetMus(UserId, propertyItems);
        }


        /// <summary>
        /// 获得墓的详细信息
        /// </summary>
        /// <param name="NetNumId"></param>
        /// <returns></returns>
        public NetMu_Model getNetMuDetailInfo(int NetMuId)
        {
            return dal.getNetMuDetailInfo(NetMuId);
        }

        /// <summary>
        /// 删除墓(物理删除)
        /// </summary>
        /// <param name="NetNuId"></param>
        /// <returns></returns>
        public int DeleteNetMu(int NetMuId)
        {
            return dal.DeleteNetMu(NetMuId);
        }

        /// <summary>
        /// 隐藏墓(逻辑删除)
        /// </summary>
        /// <param name="NetMuId"></param>
        /// <returns></returns>
        public int HiddenNetMu(int NetMuId)
        {
            return dal.HiddenNetMu(NetMuId);
        }



        /// <summary>
        /// 审核通过或者取消审核 type=1 代表审核通过 type=0代表取消审核通过
        /// </summary>
        /// <param name="type"></param>
        /// <param name="NetMuId"></param>
        /// <returns></returns>
        public int CheckPassOrCancle(int type, int NetMuId)
        {
            return dal.CheckPassOrCancle(type, NetMuId);
        }

        /// <summary>
        /// 修改公墓信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int updateNetMu(NetMu_Model model)
        {
            return dal.updateNetMu(model);
        }

        /// <summary>
        /// 设置推荐或者取消推荐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置推荐 2表示取消推荐</param>
        /// <returns></returns>
        public int SetIntro(int Id, int CancleOrIntro)
        {
            return dal.SetIntro(Id, CancleOrIntro);
        }

        /// <summary>
        /// 审核或者取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示审核 0表示取消审核</param>
        /// <returns></returns>
        public int SetCheck(int Id, int CancleOrIntro)
        {
            return dal.SetCheck(Id, CancleOrIntro);

        }

        public DataTable GetDataTable(string filter)
        {
            return dal.GetDataTable(filter);
        }

        /// <summary>
        /// 根据当前馆号,生成用模板
        /// </summary>
        /// <param name="NumStr">馆号</param>
        /// <param name="chel"></param>
        /// <param name="systemConfig">系统配制信息</param>
        /// <param name="htmlpath">生成的路径</param>
        /// <returns></returns>
        public bool CurrTempleContent(string NumStr, Channel_Type chel, SiteConfigInfo systemConfig, string htmlpath)
        {
            bool Result = false;
            StringBuilder stb = new StringBuilder();
            NetMu_Model md = new NetMu_Model();
            try
            {

                DataTable dt = GetDataTable(" Muid='" + NumStr + "'");
                dt.TableName = "NetMu";
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        md = getNetMuDetailInfo(StringHelper.isNumber_int(dr["id"].ToString()));
                        break;
                    }
                    if (md.TemplateId > 0)
                    {
                        Temple_Model TmpMd = TempleBll.CreateNew.GetModel(md.TemplateId);
                        if (TmpMd.Id > 0)
                        {
                            string TempleUrl = systemConfig.InstallDir + StringHelper.ReadConfig("TempleSkin") + chel.ToString() + "/" + TmpMd.TemplePath + "/index.html";
                            string Templepath = HttpContext.Current.Server.MapPath(TempleUrl);//模板的真实路径

                            StringBuilder newstr = new StringBuilder();
                            newstr.AppendLine("<script>");
                            newstr.AppendLine("<!--");
                            newstr.AppendLine("var Numid='" + md.MuId + "';");//陵园馆号
                            newstr.AppendLine("var id='" + md.Id + "';");//陵园ID
                            newstr.AppendLine("var UserID='" + md.CreateId + "';");//陵园名称
                            newstr.AppendLine("var Pid='" + md.Province + "';");//省份
                            newstr.AppendLine("var CityID='" + md.City + "';");//城市
                            newstr.AppendLine("-->");
                            newstr.AppendLine("</script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/jquery.js'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/Cemetery.Aspx'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/Cememtery_AddMsg.aspx'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/Cememtery_AddMsg.aspx'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/Cemetery_load.aspx?id=" + md.Id + "&rnd=" + new Random().Next(1000, 9999999) + "'></script>");
                            stb = TempleBll.CreateNew.WriteHtml(Templepath, newstr.ToString());//读取模板内容
                            stb = Script_Lable.ScriptLable_Replace(stb, chel, md.Id.ToString());

                            GetStringData_lable lbReplace = new GetStringData_lable();
                            string ContentValue = lbReplace.GetResultStr(stb, dt);
                            Result = TempleBll.CreateNew.CreateHtmlFile(ContentValue, htmlpath);
                        }
                    }
                }
            }
            catch
            {
                Result = false;
            }

            return Result;
        }


        /// <summary>
        /// 设置背景音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public int SetMusic(int Id, int MusicId)
        {
            return dal.SetMusic(Id, MusicId);

        }
        public void filterUpdate(string Upfilter, string filter)
        {
            dal.filterUpdate(Upfilter, filter);
        }


    }
}
